package com.wtwd.campus.jdzc.service.impl;

import com.wtwd.campus.jdzc.common.Constant;
import com.wtwd.campus.jdzc.dao.JdzcStudentMapper;
import com.wtwd.campus.jdzc.dao.JdzcUserStudentPermissionMapper;
import com.wtwd.campus.jdzc.entity.JdzcStudent;
import com.wtwd.campus.jdzc.service.JdzcStudentService;
import com.wtwd.campus.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author mjy
 * @date 2022/02/15
 * @description
 */
@Service
public class JdzcStudentServiceImpl implements JdzcStudentService {

    @Autowired
    private JdzcStudentMapper jdzcStudentMapper;


    @Autowired
    private RedisService redisService;


    @Autowired
    private JdzcUserStudentPermissionMapper jdzcUserStudentPermissionMapper;

    @Override
    public List<Map<String, Object>> getStuByNameAndStuNo(String name, String stuNo) {
        return jdzcStudentMapper.getStuByNameAndStuNo(name, stuNo);
    }

    @Override
    public List<Map<String,Object>> getSchoolOrgCodeListByStuIdList(List<Integer> stuIdList) {
        return jdzcStudentMapper.getSchoolOrgCodeListByStuIdList(stuIdList);
    }

    @Override
    public List<Map<String, Object>> bindStudentPhoneList(Integer studentId) {
        return jdzcStudentMapper.bindStudentPhoneList(studentId);
    }

    @Override
    public Integer updateStudentWxUserBindStatus(int state, Integer studentId) {
        return jdzcStudentMapper.updateStudentWxUserBindStatus(state,studentId);
    }

    @Override
    public int unbindStudent(Integer userId, Integer studentId) {
        return jdzcUserStudentPermissionMapper.delWxUserStudentPermission(studentId, userId);
    }

    @Override
    public Map<String, Object> getSchoolIdAndClassIdByStuId(Integer stuId) {
        return jdzcUserStudentPermissionMapper.getSchoolIdAndClassIdByStuId(stuId);
    }

    @Override
    public List<String> getImeiListByStuIdList(List<Integer> stuIdList) {
        return jdzcStudentMapper.getImeiListByStuIdList(stuIdList);
    }

    @Override
    public Map<String, Object> getStudentByStuId(Integer stuId) {
        return jdzcStudentMapper.getStudentByStuId(stuId);
    }

    @Override
    public List<JdzcStudent> getStudentListByClassId(Integer classId) {
        return jdzcStudentMapper.getStudentListByClassId(classId);
    }


    @Override
    public List<Map<String, Object>> getStudentList(Integer userId) {
        List<Map<String, Object>> studentList = jdzcStudentMapper.studentList(userId);
        Long time = System.currentTimeMillis();
        studentList.forEach(map -> {
            if (map.containsKey("imei")) {
               /* if (time - (long) map.get("last_up_time") > Constants.OFF_LINE_TIME) {
                    map.put("status", Constants.WX_DEVICE_OFFLINE);
                } else {
                    map.put("status", Constants.WX_DEVICE_ONLINE);
                }*/
                boolean flag = deviceOnline((String) map.get("imei"));
                if (flag) {
                    map.put("status", Constant.WX_DEVICE_ONLINE);
                } else {
                    map.put("status", Constant.WX_DEVICE_OFFLINE);
                }
            } else {
                map.put("status", Constant.WX_DEVICE_NOT_FOUND);
            }
        });
        return studentList;
    }


    /**
     * 判断设备是否在线
     *
     * @param imei 设备imei号
     * @return 设备是否在线
     */
    public boolean deviceOnline(String imei) {
        boolean isOnline = redisService.hasKey(Constant.DEVICE_ONLINE_PREFIX + imei);
        return isOnline;
    }

    /**
     * 获取当前时间的时分秒（HHmmss）
     *
     * @return
     */
    public String getCurTime() {
        Date d = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat("HH:mm:ss");
        String format = sbf.format(d);
        String[] split = format.split(":");
        String time = split[0].concat(split[1]).concat(split[2]);
        return time;
    }


}
